/**
 * 获取岗位列表
 * 
 * /general/internship/list
 */

 const Companies = require('../../../models/companys')
 const $ = require('../../../utils/common')
 const utils = require('../../../utils/index')
 const Sequelize = require('sequelize')
 const MySQLManager = require('../../../utils/MySQLManager');
 const sequelize = MySQLManager
 const Op = Sequelize.Op

 const get_list = async function(req, res) {
    try{

        // check company_year
        if (!req.query.company_year) {
            $.sendMessage(res, 1015, '参数company_year错误')
            return
        }

        if (!req.query.type) {
            $.sendMessage(res, 1015, '参数type错误')
            return
        }

        var query = {}
        if (req.query.company_name) {query['company_name'] = req.query.company_name}
        if (req.query.job_name) {query['job_name'] = req.query.job_name}

        var offset = 1
        var limit = parseInt(req.query.size)
        if (req.query.page > 1) {
            offset = parseInt(req.query.size) * (parseInt(req.query.page) - 1)
        } else if (req.query.page == 1) {
            offset = 0
        }
        var sql_res = {}

        if (req.query.type == '2') {
            // 模糊搜索
            sql_res = await Companies.findAndCountAll({
                order: [
                    ['type', 'DESC'],
                ],
                offset: offset,
                limit: limit,
                where: {
                    type: {
                        [Op.or]: ['1', '2']
                    },
                    company_year: req.query.company_year,
                    company_name: {
                        [Op.like]: '%' + (query.company_name == undefined ? '' : query.company_name) + '%'
                    },
                    job_name: {
                        [Op.like]: '%' + (query.job_name == undefined ? '' : query.job_name) + '%'
                    },
                },
                attributes: ['id', 'type',  'company_name', 'job_name', 'student_limit', 'current_student', 'salary'],
            })

        } else {
            // 模糊搜索
            sql_res = await Companies.findAndCountAll({
                offset: offset,
                limit: limit,
                where: {
                    type: req.query.type,
                    company_year: req.query.company_year,
                    company_name: {
                        [Op.like]: '%' + (query.company_name == undefined ? '' : query.company_name) + '%'
                    },
                    job_name: {
                        [Op.like]: '%' + (query.job_name == undefined ? '' : query.job_name) + '%'
                    },
                },
                attributes: ['id', 'type',  'company_name', 'job_name', 'student_limit', 'current_student', 'salary'],
            })
        }

        res.json({
            code: 200,
            message: 'success',
            data: {
                list: sql_res.rows,
                total: sql_res.count
            }
        })

        utils.create_a_log(req, '年度岗位', '浏览岗位列表')
    }
    catch(error) {
        $.log('error :', error)
        res.json({
            code: 0000,
            message: '抓到异常',
            error: 'error' + error
        })
        utils.create_a_log(req, '年度岗位', '浏览岗位列表', '失败')
    }
 }

 module.exports = get_list